<template>
  <div class="question-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 头部区域 -->
        <el-row>
          <el-col :span="12">
            <div
              class="grid-content bg-purple"
              style="font-size: 12px; color: pink"
            >
              说明：目前支持学科和关键字条件筛选
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light" style="float: right">
              <el-button
                type="success"
                size="small"
                class="btn_wrapper"
                @click="$router.push({path:'./new', query:{id:scope.row.id}})"
              ><i class="el-icon-edit" />新增试题</el-button>
            </div>
          </el-col>
        </el-row>
        <!-- 筛选 -->
        <el-form ref="form" :model="requestParams" label-width="80px" size="small">
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select v-model="requestParams.subjectID" style="width:100%" @change="getcatalogID">
                  <el-option
                    v-for="item in subjectOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select
                  v-model="requestParams.catalogID"
                  style="width:100%"
                >
                  <el-option
                    v-for="item in catalogOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select v-model="requestParams.tags" style="width:100%">
                  <el-option
                    v-for="item in tagOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input v-model="requestParams.keyword" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select
                  v-model="requestParams.questionType"
                  style="width:100%"
                >
                  <el-option
                    v-for="item in questionTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  /> </el-select></el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select
                  v-model="requestParams.difficulty"
                  style="width:100%"
                >
                  <el-option
                    v-for="item in diffOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  /> </el-select></el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select v-model="requestParams.direction" style="width:100%">
                  <el-option
                    v-for="item in directionOptions"
                    :key="item"
                    :label="item"
                    :value="item"
                  /> </el-select></el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select v-model="requestParams.creatorID" style="width:100%">
                  <el-option
                    v-for="item in creatorOptions"
                    :key="item.id"
                    :label="item.label"
                    :value="item.username"
                  /> </el-select></el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input v-model="requestParams.remarks" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input v-model="requestParams.shortName" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-select
                  v-model="requestParams.province"
                  class="filter-item"
                  style="width: 50%"
                  clearable
                  @change="handleCity"
                >
                  <el-option
                    v-for="item in cityOptions"
                    :key="item"
                    :label="item"
                    :value="item"
                  />
                </el-select>
                <el-select
                  v-model="requestParams.city"
                  class="filter-item"
                  style="width: 50%"
                  clearable
                >
                  <el-option
                    v-for="item in areaOptions"
                    :key="item"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item style="text-align:right">
                <el-button @click="reset()">清除</el-button>
                <el-button type="primary" @click="getQuestionsList">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <!-- 列表 -->
        <el-table
          v-loading="isLoading"
          :data="questionsList"
          element-loading-text="给我一点时间"
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column label="试题编号" prop="number" width="120" />
          <el-table-column label="学科" prop="subject" />
          <el-table-column label="题型">
            <template v-slot="scope">
              {{ questionTypeOptions.find(item => item.value === +(scope.row.questionType)).label }}
            </template>
          </el-table-column>
          <el-table-column label="题干" width="300">
            <template slot-scope="scope">
              <span v-html="scope.row.question" />
            </template>
          </el-table-column>
          <el-table-column label="录入时间" width="180">
            <template slot-scope="scope">
              {{ scope.row.addDate|parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column label="难度">
            <template slot-scope="scope">
              {{ diffOptions.find(item=>item.value===+scope.row.difficulty).label }}
            </template>
          </el-table-column>
          <el-table-column label="录入人" prop="creator" />
          <el-table-column
            align="center"
            label="操作"
            width="200"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button plain type="success" icon="el-icon-view" circle title="预览" @click="viewItem(scope.row)" />
              <el-button plain type="primary" icon="el-icon-edit" circle title="修改" @click="$router.push({path:'./new', query:{id:scope.row.id}})" />
              <el-button plain type="danger" icon="el-icon-delete" circle title="删除" @click="delItem(scope.row)" />
              <el-button plain type="warning" icon="el-icon-check" circle title="加入精选" @click="addChoice(scope.row)" />
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          style="margin-top:20px;text-align:right"
          background
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          :page-size="requestParams.pagesize"
          :current-page="requestParams.page"
          :page-sizes="[5,10,15,20]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        title="题目预览"
        width="50%"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <topic-preview v-if="dialogVisible" :tp-id="data.id" :tp-type="questionType" @close="close" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { simple as getSubjectList } from '../../api/hmmm/subjects'
import { simple as getDirectoryList } from '../../api/hmmm/directorys'
import { simple as getUserList } from '../../api/base/users'
import { difficulty, questionType, direction } from '../..//api/hmmm/constants'
import { simple as getTagList } from '../../api/hmmm/tags'
import { provinces as getCitys, citys as getArea } from '../../api/hmmm/citys'
import { list as getQuestionList, remove as delQuestion, choiceAdd } from '../../api/hmmm/questions'
import { formatTime } from '../../utils'

export default {
  data() {
    return {
      questionType: null, // 试题类型
      dialogVisible: false,
      questionsList: [], // 题库列表
      requestParams: {
        subjectID: null, // 学科
        catalogID: null, // 二级目录
        tags: null, // 标签
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null,
        city: null,
        page: 1,
        pagesize: 5
      },
      // 学科选项
      subjectOptions: [],
      // 二级目录选项 当选择了学科的时候赋值
      catalogOptions: [],
      // 标签选项 当选择了学科的时候赋值
      tagOptions: [],
      // 试题类型选项
      questionTypeOptions: [],
      // 难度选项
      diffOptions: [],
      // 方向选项
      directionOptions: direction,
      // 录入人选项
      creatorOptions: [],
      // 城市选项
      cityOptions: getCitys(),
      // 地区选项 当选择了城市的时候赋值
      areaOptions: [],
      total: 0, // 总条数
      isLoading: false
    }
  },
  async created() {
    this.diffOptions = difficulty
    this.questionTypeOptions = questionType
    // 获取学科列表
    const { data: subjectArr } = await getSubjectList()
    this.subjectOptions = subjectArr

    // 获取录入人列表
    const { data: userArr } = await getUserList()
    this.creatorOptions = userArr

    // 默认加载题库列表
    this.getQuestionsList()
  },
  methods: {
    // 关闭弹框
    close() {
      this.dialogVisible = false
    },
    // 预览
    viewItem(data) {
      this.questionType = data.questionType
      // 打开弹框
      this.dialogVisible = true
      this.data = data
    },
    // 删除
    async delItem(data) {
      const delRes = await this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (delRes === 'cancel') return this.$message('您已取消删除')
      // 调用删除接口
      await delQuestion(data)
      // console.log(res)
      // 判断当前数据的长度是否等于 1
      if (this.questionsList.length === 1) {
        this.requestParams.page--
        // 如果是第一页的最后一条
        if (this.requestParams.page <= 0) {
          this.requestParams.page = 1
        }
      }
      this.$message.success('删除成功')
      // 重新渲染列表
      this.getQuestionsList()
    },
    // 加入精选
    async addChoice(data) {
      await this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).catch(err => err)
      // 调用加入精选接口  choiceState 1:加入
      data.choiceState = 1
      await choiceAdd(data)
      this.$message.success('加入精选成功')
      this.getQuestionsList()
    },
    // 清除表单
    reset() {
      this.requestParams = {
        subjectID: null, // 学科
        catalogID: null, // 二级目录
        tags: null, // 标签
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注id
        shortName: null, // 企业简称
        province: null,
        city: null
      }
      this.catalogOptions = []
      this.tagOptions = []
    },
    formatTime, // 格式化时间
    // 根据选择的学科 获取联动表单
    async getcatalogID(subjectID) {
      this.requestParams.catalogID = null
      // 获取二级目录
      const { data: catalogArr } = await getDirectoryList({ subjectID })
      this.catalogOptions = catalogArr
      // 获取标签
      const { data: tagArr } = await getTagList({ subjectID })
      this.tagOptions = tagArr
    },
    // 获取可选地区列表
    handleCity(value) {
      // 地区选中的数据清空
      this.requestParams.city = null
      this.areaOptions = getArea(value)
    },
    // 获取题库列表
    async getQuestionsList() {
      const res = await getQuestionList(this.requestParams)
      // console.log(res)
      this.questionsList = res.data.items
      this.total = res.data.counts
      this.requestParams.page = 1
    },
    // 当页码发生改变时触发
    handleCurrentChange(newPage) {
      this.requestParams.page = newPage
      this.getQuestionsList()
    },
    // 当每页显示条数发生改变时触发
    handleSizeChange(newSize) {
      this.requestParams.pagesize = newSize
      this.getQuestionsList()
    }
  }
}
</script>

<style lang="scss" scoped>
.question-container {
  padding: 0 10px;
  margin: 10px 0;
}
.btn_wrapper {
  margin-bottom: 15px;
}

</style>
